package com.sdy.service.Impl;

import com.sdy.dao.mysql.OverViewDao;
import com.sdy.dao.mysql.ParmMapper;
import com.sdy.po.OverView;
import com.sdy.po.Parm;
import com.sdy.service.OverViewService;
import com.sdy.util.OverViewUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class OverViewServiceImpl implements OverViewService {

    @Resource
    ParmMapper parmMapper;
    @Resource
    OverViewUtil overViewUtil;
    @Resource
    OverViewDao overViewDao;

    /**
     * 通过传来的起始日期和日期段查找设备运行的信息
     * @param startDate
     * @return
     */
    @Override
    public Map<String,Object> findOverViewByDate(Integer id, Date startDate, Integer after) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        List<String> keyList = new ArrayList<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (int i = 0; i < after; i++) {
            Date date = cal.getTime();
            if (date.compareTo(new Date())>0){
                break;
            }
            keyList.add("overView:" + id + ":" + simpleDateFormat.format(date));
            cal.add(Calendar.DATE,1);
        }
        System.out.println(keyList);
        Map<String,Object> overViewsInformation = overViewUtil.getOverViewsInformation(overViewDao.findOverViewByKeys(keyList));
        return overViewsInformation;
    }

    /**
     * 查找信息种类名称
     * @return
     */
    @Override
    public List<Parm> findAllOverViewName() {
        return parmMapper.selectAllParm();
    }

    /**
     * 判断更新还是上传
     * @param overView
     * @param set
     * @return
     */
    public Boolean addOverView(OverView overView,Boolean set){
        String key = getKey(overView);
        System.out.println(set);
        if (set){
            System.out.println("走set");
            if (!ifHasKey(getKey(overView))){
                System.out.println("没有这个key");
                return overViewDao.addOverView(overView, key);
            }
            else {
                System.out.println("有这个key");
                return false;
            }
        }
        else {
            System.out.println("走update");
            return overViewDao.addOverView(overView,key);
        }
    }

    @Override
    public Boolean updateOverView(String key, OverView overView) {
        return null;
    }

    @Override
    public Boolean ifHasKey(String key) {
        if (overViewDao.ifHasKey(key)){
            System.out.println("有这个");
            return true;
        }
        return false;
    }

    @Override
    public String getKey(OverView overView) {
        return  "overView:" + overView.getId() + ":" + new SimpleDateFormat("yyyy-MM-dd").format(overView.getUpdateTime());
    }

}
